package com.lam.code.controller;

import cn.hutool.core.lang.Assert;
import com.lam.code.base.BaseController;
import com.lam.code.entity.User;
import com.lam.code.service.HelloService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author lam
 * @description TODO
 * @date 2019/9/29 22:43
 */
//@Controller
@Slf4j
//@Api(tags="测试控制器")
public class HelloController extends BaseController<HelloService, User> {
    @GetMapping("/insertUser")
    @ResponseBody
    public String insertUser() {
        User user = new User();
        user.setAge(18);
        user.setUsername("张三");
        user.setEmail("132@qq.com");
        this.service.save(user);
        log.info("插入用户");
        return "save success";
    }

    @GetMapping("/toUser")
    public String toUser() {
        return "toUser";
    }


    /**
     * 主库读取（默认）
     * @return
     */
    @GetMapping("/getUsers")
    @ResponseBody
   // @ApiOperation(httpMethod = "GET",value = "获取所有用户")
    public List<User> getUsers() {
        log.info("获取所有用户");
        return this.service.getUser();
    }

    /**
     * 从库读取
     * @return
     */
    @GetMapping("/getUsers2")
    @ResponseBody
    // @ApiOperation(httpMethod = "GET",value = "获取所有用户")
    public List<User> getUsers2() {
        log.info("获取所有用户");
        return this.service.getUser2();
    }

    @GetMapping("/getUser/{id}")
    @ResponseBody
    public User getUser(@PathVariable Long id) {
        Assert.notNull(id,"参数id为空");
        log.info("获取单个用户");
       return service.getById(id);
    }

}
